package com.rp.common.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 类说明:出参定义
 *
 * @author ****
 * @date 2019-08-29 14:20  新建
 */
@ApiModel(description = "接口出参对象")
@Data
@Accessors(chain = true)
public class ResultDTO<T> implements Serializable {

    @ApiModelProperty(value = "是否请求成功")
    private Boolean result;

    @ApiModelProperty(value = "提示信息")
    private String message;

    @ApiModelProperty(value = "编码/状态")
    private Integer code;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "额外")
    private Object extra;

    @ApiModelProperty(value = "表名", hidden = true)
    @JsonIgnore
    private String onlTable;


    public ResultDTO() {
        this("ok");
    }

    public ResultDTO(String message) {
        this(true, message, 200);
    }

    public ResultDTO(boolean result, String message) {
        this(result, message, 200);
    }

    public ResultDTO(boolean result, String message, int code) {
        this(result, message, code, null);
    }

    public ResultDTO(boolean result, String message, int code, T data) {
        this(result, message, code, data, null);
    }

    public ResultDTO(boolean result, String message, int code, T data, Object extra) {
        this.result = result;
        this.message = message;
        this.code = code;
        this.data = data;
        this.extra = extra;
    }

    public ResultDTO message(String message) {
        this.message = message;
        return this;
    }

    public ResultDTO data(T data) {
        this.data = data;
        return this;
    }

    public ResultDTO extra(String extra) {
        this.extra = extra;
        return this;
    }

    public static ResultDTO tf(boolean boo) {
        if (boo) {
            return ResultDTO.success();
        } else {
            return ResultDTO.error().message("操作失败");
        }
    }

    public static ResultDTO success() {
        return new ResultDTO(true, "ok", 200);
    }

    public static ResultDTO bad() {
        return new ResultDTO(false, "bad request", 417);
    }

    public static ResultDTO error() {
        return new ResultDTO(false, "请联系管理员", 500);
    }

    //  参数异常 1001 - 1999
    public static ResultDTO PARAM_ERROR() {
        return new ResultDTO(false, "参数错误", 1001);
    }

    public static ResultDTO PARAM_IS_INVALID() {
        return new ResultDTO(false, "参数无效", 1002);
    }

    public static ResultDTO PARAM_IS_BLANK() {
        return new ResultDTO(false, "参数为空", 1003);
    }

    public static ResultDTO PARAM_TYPE_BIND_ERROR() {
        return new ResultDTO(false, "参数类型错误", 1004);
    }

    public static ResultDTO PARAM_NOT_COMPLETE() {
        return new ResultDTO(false, "参数缺失", 1005);
    }

    //  用户错误 2001 - 2999
    public static ResultDTO USER_NOT_LOGGED_IN() {
        return new ResultDTO(false, "用户未登录", 2001);
    }

    public static ResultDTO USER_LOGIN_ERROR() {
        return new ResultDTO(false, "用户名或密码不正确", 2002);
    }

    public static ResultDTO USER_ACCOUNT_FORBIDDEN() {
        return new ResultDTO(false, "账号已禁用,请联系管理员", 2003);
    }

    public static ResultDTO USER_NOT_EXIST() {
        return new ResultDTO(false, "用户不存在", 2004);
    }

    public static ResultDTO LOG_IN_TO_AN_ACCOUNT_SOMEWHERE_ELSE() {
        return new ResultDTO(false, "账号在其他地方的登录", 2005);
    }

    // 权限错误 3001 - 3999
    public static ResultDTO PERMISSION_ERROR() {
        return new ResultDTO(false, "权限认证失败", 3001);
    }

    public static ResultDTO PERMISSION_NOT() {
        return new ResultDTO(false, "没有权限", 3002);
    }

    // 访问频繁限制 4001
    public static ResultDTO ACCESS_RESTRICTED() {
        return new ResultDTO(false, "访问限制", 4001);
    }

}
